var addrSelect;//加载地区

//        <%--加载地区 start--%>
function loadAddr() {
    //return  $(".sel").ld({ajaxOptions : {"url" : dataValue.webRoot+"/themes/simplebootx/Public/assets/addressBook.json"},
    //            defaultParentId:9,style:{"width": 100}
    //        });
}
//        <%--加载地区 end--%>
//        <%--初始化 设置btnAdd监听 start--%>
$(document).ready(function() {
    $("input").keypress(function(){
        $("#alert").hide()
        $("#alert1").hide()
        $("#alert2").hide()
        $("#alert3").hide()
        $("#alert4").hide()
        $("#alert5").hide()
    })
    addrSelect = loadAddr();
    jQuery("#btnAdd").click(function() {
        var name = jQuery.trim(jQuery("#name").val());
        var mobile = jQuery.trim(jQuery("#mobile").val());
        var tel = jQuery.trim(jQuery("#tel").val());
        var province = jQuery.trim(jQuery("#province").val());
        var city = jQuery.trim(jQuery("#city").val());
        var area = jQuery.trim(jQuery("#area").val());
        var addr = jQuery.trim(jQuery("#addr").val());
        var zipCode = $("#zipcode").val();
        var receiveAddrId = jQuery.trim(jQuery("#receiveAddrId").val());

        name = name.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig, "");
        name = name.replace(/<.*?>/g, "");
        addr = addr.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig, "");
        addr = addr.replace(/<.*?>/g, "");
        mobile = mobile.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig, "");
        mobile = mobile.replace(/<.*?>/g, "");
        tel = tel.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig, "");
        tel = tel.replace(/<.*?>/g, "");

        if (name == "" || name == null) {
            alertMsg("请输入收货人姓名","#alert")
            return;
        }else{
            $("alert").hide();
        }
        if(name.length<2||name.length>20){
            alertMsg("收货人姓名的长度为2-20个字","#alert")
            return;
        }

        if(tel == "" || tel== null){
            if (mobile == "" || mobile == null) {
                alertMsg("手机和固定电话至少有一项必填","#alert1")
                return;
            }else{

                $("#alert1").hide()
            }
        }
        if(mobile == "" || mobile == null){
            if (tel == "" || tel == null){
                alertMsg("手机和固定电话至少有一项必填","#alert2")
                return;
            }else{
                $("#alert2").hide()
            }
        }else{
            var a = /^((\(\d{3}\))|(\d{3}\-))?13\d{9}|14[57]\d{8}|15\d{9}|18\d{9}$/ ;
            if(mobile.length != 11){
                alertMsg("请输入正确的手机号码","#alert1");
                return;
            }
            if(!mobile.match(a)) {
                alertMsg("请输入正确的手机号码","#alert1");
                return;
            }
        }


        if (addr == "" || addr == null) {
            alertMsg("地址不能为空","#alert4")
            return;
        }else{
            $("#alert4").hide()
        }

        if (zipCode == "" || zipCode == null) {
            alertMsg("邮政编码不能为空","#alert5")
            return;
        }else{
            var re= /^[1-9][0-9]{5}$/
            if(!re.test(zipCode)){
                alertMsg("请输入正确的邮政编号","#alert5")
                return;
            }
            $("alert5").hide();
        }

//            var params = $("#newAddrForm").formToArray();
        $.ajaxSettings['contentType'] = "application/x-www-form-urlencoded; charset=utf-8";
        $.ajax({
                    type:"POST",
                    url:dataValue.webRoot+"/index.php?g=product&m=member&a=address_save",
                    data: {name:name,
                        addr:addr,
                        mobile:mobile,
                        zipcode:zipCode,
                        tel:tel,
                        province :province,
                        city :city,
                        area :area,
                        receiveAddrId:receiveAddrId
                    },
                    dataType: "json",
                    success:function(data) {
                        alert("保存地址成功");
                        location.reload();
                        jQuery("#name").attr("value", "");
                        jQuery("#mobile").attr("value", "");
                        jQuery("#tel").attr("value", "");
                        jQuery("#zipcode").attr("value", "");
                        jQuery("#addr").attr("value", "");
                        jQuery("#receiveAddrId").attr("value", "");
                    },
                    error:function(XMLHttpRequest, textStatus) {
                        alert("保存地址成功");
                        location.reload();
                    }
                });
    });
})
//        <%--初始化 设置btnAdd监听 end--%>

//        <%--编辑地址 start--%>
function btnAlt(reciverAddrId) {
    var zoneId;
    $.ajax({
                type:"post" ,
                url:dataValue.webRoot+"/index.php?g=product&m=member&a=get_address",
                data:{id:reciverAddrId},
                dataType:"json",
                success:function(data) {
                    jQuery("#name").attr("value", data.name);
                    jQuery("#mobile").attr("value", data.mobile);
                    jQuery("#tel").attr("value", data.tel);
                    jQuery("#zipcode").attr("value", data.zipcode);
                    jQuery("#addr").attr("value", data.addr);
                    jQuery("#receiveAddrId").attr("value", data.id);
                    //jQuery("#province").val(data.province);
                    //jQuery("#city").val(data.city);
                    //jQuery("#area").val(data.area);
                    $('#target').distpicker({
                        province: '浙江省',
                        city: '杭州市',
                    });
                }
            });
}
//        <%--编辑地址 end--%>

//        <%--设置地区名称 start--%>
function setAddrNm(zoneId) {
    $.ajax({
                type:"post" ,url:dataValue.webRoot+"/themes/simplebootx/Public/assets/zoneNm.json",
                data:{zoneId:zoneId},
                dataType:"json",
                success:function(data) {
                    var defaultValue = [data.provinceNm,data.cityNm,data.zoneNm];
                    addrSelect.ld("api").selected(defaultValue)
                }
            })
}
//        <%--设置地区名称 end--%>

//        <%--删除地址 start--%>
function btnDel(reciverAddrId) {
    if(confirm('是否将此联系人信息删除?')){
        $.ajax({
            type:"post" ,
            url:dataValue.webRoot+"/index.php?g=product&m=member&a=delete_address&id=" + reciverAddrId ,
            success:function() {
                alert("删除地址成功");
                location.reload();
            }
        });
    }
}
//        <%--删除地址 end--%>

//        <%--提示控制 start--%>
function alertMsg(errorMsg,element){
    $(element).fadeIn(500);
    $(element).html(errorMsg);
}
//        <%--提示控制 end--%>